package com.yumei.trader.gym.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

@SuppressLint("SimpleDateFormat")
public class DateUtil {

	@SuppressLint("SimpleDateFormat")
	private static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");

	private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd");
	@SuppressLint("SimpleDateFormat")
	private static SimpleDateFormat longformat = new SimpleDateFormat("yyyyMMddHHmmss");
	
	private static SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm");
	
	private static SimpleDateFormat dtformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	private static SimpleDateFormat dtsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static SimpleDateFormat enformat =new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);


	private static String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};

	@SuppressLint("DefaultLocale")
	public static String getDateTimeInMillis(long millisecond){

		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);

		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);

		return String.format("%02d", month) + "/" + String.format("%02d", date);
	}

	@SuppressLint("DefaultLocale")
	public static String getHourTimeInMillis(long millisecond){

		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);

		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);

		return String.format("%02d", hour) + ":" + String.format("%02d", minute);
	}

	@SuppressLint("DefaultLocale")
	public static String getDateTimeInMillis1(long millisecond){

		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);

		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);

		return year + "/" + String.format("%02d", month) + "/" + String.format("%02d", date);
	}

	@SuppressLint("DefaultLocale")
	public static String getDateTimeInMillis3(long millisecond){

		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);

		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);

		return year + "." + month + "." + date;
	}

	@SuppressLint("DefaultLocale")
	public static String getDateTimeInMillis2(long millisecond){

		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);

		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);

		return String.format("%02d", month) + "." + String.format("%02d", date);
	}

	@SuppressLint("DefaultLocale")
	public static String getYearMonthDay(long millisecond){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);
		return year + "年" +String.format("%02d", month) + "月" + String.format("%02d", date) + "日";
	}

	@SuppressLint("DefaultLocale")
	public static String getMonthDay(long millisecond){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);
		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);
		return String.format("%02d", month) + "月" + String.format("%02d", date) + "日";
	}

	@SuppressLint("DefaultLocale")
	public static String getMonthDayHourMinute(long millisecond){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);
		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		return String.format("%02d", month) + "-" + String.format("%02d", date) + " " +
				String.format("%02d", hour) + ":" + String.format("%02d", minute);
	}

	@SuppressLint("DefaultLocale")
	public static String getMonthDayAddTime(long millisecond){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);
		int month = calendar.get(Calendar.MONTH)+1;
		int date = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		return String.format("%02d", month) + "月" + String.format("%02d", date) + "日 " +
				String.format("%02d", hour) + ":" + String.format("%02d", minute);
	}

	/**
	 * 将毫秒转化成固定格式的时间
	 * 时间格式: yyyy-MM-dd
	 * @param millisecond 毫秒数
	 * @return 时间字符串
	 */
	public static String getDateTimeFromMillisecond(long millisecond){
		Date date = new Date(millisecond);
		return dateformat.format(date);
	}

	/**
	 * 将毫秒转化成固定格式的时间
	 * 时间格式: yyyy-MM-dd HH:mm:ss
	 * @param millisecond 毫秒数
	 * @return 时间字符串
	 */
	public static String getDateTimeMinuteFromMillisecond(long millisecond){
		Date date = new Date(millisecond);
		return dtsFormat.format(date);
	}

	public static String getCurrentStr(){

		return toDateAndTimeStr(Calendar.getInstance());
	}

	//转成09:00
	public static String toTimeStr(Calendar c){
		
		return " "+timeformat.format(c.getTime());
	}

	//转成2015-01-01
	public static String toDateStr(Calendar c){
		
		return dateformat.format(c.getTime());
	}

	//转成2015-01-01
	public static String toDateStr(Date d){
		return dateformat.format(d);
	}

	//转成20150101
	public static String toLongDateStr(Calendar c){
		
		return longformat.format(c.getTime());
	}

	//转成20150101
	public static String toLongDateStr(Calendar c,int days){
		
		c.add(Calendar.DAY_OF_MONTH, days);
		return longformat.format(c.getTime());
	}

	//转成2015-01-01 09:00:00
	public static String toDateAndTimeStr(Calendar c){
		return dtformat.format(c.getTime());
	}
	
	public static String toDateAndTimeStr(Date d){
		return dtformat.format(d);
	}



	//2015-01-01 09:00:00 转Date
	public static Date strToDate(String str){
		
		try{
			return dtformat.parse(str);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("strToDate exception ");
		}
		return new Date();
	}


	//2015-01-01 09:00:00 转成 Calendar
	public static Calendar strToCalendar(String str){
		
		Date date = strToDate(str);
		
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		return c;
	}

	public static Date enStrToDate(String str){
		try {
			return dtformat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	
	
	//20141205转成Date
	public static Date strLongToDate(String str) {

		try{
			return longformat.parse(str);
		}catch(Exception e){
			e.printStackTrace();
		}
		return new Date();
	}

	public static String toCustomString(Date date){

		String time = timeformat.format(date);

		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);

		long diff = new Date().getTime()- (date.getTime()/1000)*1000;
		long days = diff / (1000 * 60 * 60 * 24);

		if (days>0) {

			if (days == 1) {
				return "昨天 " + time;
			}else {
				return dateformat.format(date);
			}
		}else {
			return "今天 " + time;
		}
	}

	public static String toDifferDate2(long timeLong){

		long diff = new Date().getTime() - timeLong;

		long days = diff / (1000 * 60 * 60 * 24);
		long hour = diff / (1000 * 60 * 60 );
		long minute = diff/(1000*60);

		if(days>0){

			if(days == 1){
				return "昨天";
			}else{
				return toDateStr(new Date(timeLong));
			}

		}else{

			if(hour>0){
				return hour+"小时前";
			}else{
				if(minute>0){
					return minute+"分钟前";
				}else{
					return "1分钟前";
				}
			}
		}
	}


	public static String dateformat(String date){

		Calendar calendar = strToCalendar(date);
		Calendar today = Calendar.getInstance();

		int days = getDays(calendar, today);
		switch (days){
			case 0:
				return toTimeStr(calendar);
			case 1:
				return "昨天"+toTimeStr(calendar);
			case 2:
			 	int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
				return weeks[week] + toTimeStr(calendar);
		}

		return String.format("%02d", (calendar.get(Calendar.MONTH)+1))+"月"
				+String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH))+"日"+toTimeStr(calendar);

	}

	/**
	 * 日期友好显示
	 * @param date 需要比较的日期
	 * @return “今天,昨天,日期”
	 */
	public static String dateFormat(Date date){

        long nowDateTime = 0;

        long dateTime = date.getTime();

        try {
            nowDateTime = dtsFormat.parse(dateformat.format(new Date()) + " 00:00:00").getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (dateTime < nowDateTime){
            if (nowDateTime - dateTime > 1000*60*60*24){
                return dateFormat.format(date);
            }else {
                return "昨天";
            }
        }else {
            return "今天";
        }
	}

	public static int getDays(Calendar calendar,Calendar tody){

		for(int i=0;i<3;i++) {
			if (calendar.get(Calendar.MONTH) == tody.get(Calendar.MONTH)) {
				if (calendar.get(Calendar.DAY_OF_MONTH) == tody.get(Calendar.DAY_OF_MONTH)) {
					return i;
				}
			}
			tody.add(Calendar.DAY_OF_MONTH, -1);
		}
		return -1;
	}


	//时间差
	public static int distance(String dateStr1,String dateStr2){

		Date date1 = strToDate(dateStr1);
		Date date2 = strToDate(dateStr2);

		long cha = (date1.getTime()-date2.getTime())/1000;

		return (int)(cha/60);
	}

	/**
	 * 计算两个日期之间的周末天数
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return 工作日天数
	 * @throws ParseException
	 */
	public static int countWeekEnds(Date startDate, Date endDate) throws ParseException {
		// 初始化第一个日期
		Calendar cal1 = Calendar.getInstance();
		// 初始化第二个日期，这里的天数可以随便的设置
		Calendar cal2 = Calendar.getInstance();

		// 指定一个日期
		Date date1 = dtformat.parse(dtformat.format(startDate));
		Date date2 = dtformat.parse(dtformat.format(endDate));
		// 对 calendar 设置为 date 所定的日期
		cal1.setTime(date1);
		cal2.setTime(date2);

		int holidays = 0;
		// 确定一个 大日期
		if (cal1.compareTo(cal2) > 0) {
			Calendar temp = cal1;
			cal1 = cal2;
			cal2 = temp;
		}
		while (cal1.compareTo(cal2) <= 0) {
			if (cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
				holidays++;
			}
			cal1.add(Calendar.DAY_OF_YEAR, 1);

		}

		return holidays;
	}

	private static final String[] holidays = {"2017-01-02","2017-01-27","2017-01-30","2017-01-31",
			"2017-02-01","2017-02-02",
			"2017-04-03","2017-04-04",
			"2017-05-01","2017-05-29","2017-05-30",
			"2017-10-02","2017-10-03","2017-10-04","2017-10-05","2017-10-06"};
	private static final String[] ex_workdays = {"2017-01-22","2017-02-04","2017-04-01","2017-05-27","2017-09-30"};

	/**
	 * 获得指定时间段内的工作日总数
	 * @param begDate 起始日期
	 * @param endDate 结束日期
	 * @return 工作日总数
	 */
	public static int getWorkDays(Date begDate ,Date endDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if (begDate.after(endDate))
			return -1;
		// 总天数
		int days = (int) ((endDate.getTime() - begDate.getTime()) / (24 * 60 * 60 * 1000)) + 1;
		// 总周数，
		int weeks = days / 7;
		int rs = 0;
		// 整数周
		if (days % 7 == 0) {
			rs = days - 2 * weeks;
		}else {
			Calendar begCalendar = Calendar.getInstance();
			Calendar endCalendar = Calendar.getInstance();
			begCalendar.setTime(begDate);
			endCalendar.setTime(endDate);
			// 周日为1，周六为7
			int beg = begCalendar.get(Calendar.DAY_OF_WEEK);
			int end = endCalendar.get(Calendar.DAY_OF_WEEK);
			if (beg > end) {
				rs = days - 2 * (weeks + 1);
			} else if (beg < end) {
				if (end == 7 || beg ==1) {
					rs = days - 2 * weeks - 1;
				} else {
					rs = days - 2 * weeks;
				}
			} else {
				if (beg == 1 || beg == 7) {
					rs = days - 2 * weeks - 1;
				} else {
					rs = days - 2 * weeks;
				}
			}
		}

		int ex_days = 0;
		try {
			//节假日处理
			Date datetime;
			for(String holiday : holidays){
				datetime = sdf.parse(holiday);
				if(datetime.compareTo(begDate)==0 ||
						datetime.compareTo(endDate)==0 ||
						( datetime.after(begDate) && datetime.before(endDate) ) ){
					ex_days--;
				}
			}

			/*for(String workday : ex_workdays){
				datetime = sdf.parse(workday);
				if(datetime.compareTo(begDate)==0 ||
						datetime.compareTo(endDate)==0 ||
						( datetime.after(begDate) && datetime.before(endDate) ) ){
					ex_days++;
				}
			}*/
		} catch (ParseException e) {
			ex_days = 0;
			e.printStackTrace();
		}

		return rs+ex_days;
	}



	public static void main(String[] args){

		String str = DateUtil.dateformat("2016-03-09 15:05:09");
		System.out.println(str);
	}

	/**
	 * 使用用户格式提取字符串日期
	 *
	 * @param strDate 日期字符串
	 * @param pattern 日期格式
	 * @return
	 */

	public static Date parse(String strDate, String pattern) {

		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		try {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用用户格式格式化日期
	 *
	 * @param date    日期
	 * @param pattern 日期格式
	 * @return
	 */

	public static String format(Date date, String pattern) {
		String returnValue = "";
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

	public static boolean isSameDate(String date1, String date2) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date d1 = null;
		Date d2 = null;
		try {
			d1 = format.parse(date1);
			d2 = format.parse(date2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setFirstDayOfWeek(Calendar.MONDAY);//西方周日为一周的第一天，咱得将周一设为一周第一天
		cal2.setFirstDayOfWeek(Calendar.MONDAY);
		cal1.setTime(d1);
		cal2.setTime(d2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (subYear == 0) {
			// subYear==0,说明是同一年
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (subYear == 1 && cal2.get(Calendar.MONTH) == 11) {
			//subYear==1,说明cal比cal2大一年;java的一月用"0"标识，那么12月用"11"
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (subYear == -1 && cal1.get(Calendar.MONTH) == 11) {
			//subYear==-1,说明cal比cal2小一年
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}
}
